package com.sinodata.bsm.cicp.collector.db2;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class DB2IOCollector extends AbstractJDBCCollector {
    private static Logger logger = Logger.getLogger(DB2IOCollector.class);
    private Long[] lastValue = null;
    private long lastTime = -1;

    public DB2IOCollector() {
    }

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String dbtype = this.paramMap.get("DBType");
        String dbname = this.paramMap.get("DBName");
        String sql = this.paramMap.get("SQL");
        String[] propMetas = getPropMeta();
        Long[] tmpValues = new Long[propMetas.length];
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String start = "";
        try {
            ResultSet rs = this.getCachedResultSet(dbtype, ip, dbname, sql);
            while (rs.next()) {
                start = rs.getObject(1).toString();
                for (int i = 0; i < tmpValues.length; i++) {
                    String v = rs.getObject(i + 2).toString();
                    tmpValues[i] = Long.parseLong(v);
                }
            }
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date begin1 = df.parse(start);
            long currentTimeMillis = begin1.getTime();
            if (lastTime > 0 && lastValue != null && lastValue.length == tmpValues.length && currentTimeMillis > lastTime) {
                ResPropertyValue propertyValueX = new ResPropertyValue();
                ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[tmpValues.length];
                for (int i = 0; i < propValues.length; i++) {
                    long v = (tmpValues[i] - lastValue[i]) / ((currentTimeMillis - lastTime) / 1000);
                    propValues[i] = ResPropertyValue.createPropValue(String.valueOf(v), propMetas[i]);
                }
                propertyValueX.setValues(propValues);
                values.add(propertyValueX);
            }
            lastTime = currentTimeMillis;
            lastValue = tmpValues;
        } catch (Exception e) {
            logger.error(this.getClass().getName() + ".collect()", e);
            throw new CollectorException(this.getClass().getName() + ".collect()", e);
        }
        return values;
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "DBType", "DBName", "SQL", "DBIP" };
    }

    @Override
    public String[] getPropMeta() {
        int count = Integer.parseInt(this.paramMap.get("FieldNum"));
        String[] propMetas = new String[count];
        for (int i = 1; i <= propMetas.length; i++) {
            propMetas[i - 1] = "$" + i;
        }
        return propMetas;
    }

    /**
     * 最多只支持两位
     *
     * @param number
     * @return
     */
    /*    private static String toChineseString(int number) {
            if (number == 0) {
                return "零";
            }
            String chineseString = "";
            int n = number / 10;
            if (n > 0) {
                if (n > 1) {
                    chineseString += toChineseNumber(n);
                }
                chineseString += "十";
            }
            n = number % 10;
            if (n > 0) {
                chineseString += toChineseNumber(n);
            }
            return chineseString;
        }*/

    /*    private static String toChineseNumber(int number) {
            String[] chineseNumbers = new String[] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
            return chineseNumbers[number];
        }*/

    /**
     * @param args
     */
    public static void main(String[] args) {

    }

}
